﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            using (FlashGamesDataContext dataContext = new Manager.FlashGamesDataContext())
            {
                int alreadyCount = dataContext.UserInfos.Where(obj => (obj.Username == txtUsername.Text.Trim())).Count();
                if (alreadyCount == 0)
                {
                    UserInfo userInfo = new Manager.UserInfo()
                    {
                        Username = txtUsername.Text.Trim(),
                        CreatedDate = DateTime.Now,
                        FullName = txtName.Text,
                        Password = txtPassword.Text,
                        ImageUrl = "nopic.jpeg"
                    };

                    dataContext.UserInfos.InsertOnSubmit(userInfo);
                    dataContext.SubmitChanges();
                    txtName.Text = "";
                    txtPassword.Text = "";
                    txtReenterPassword.Text = "";
                    txtUsername.Text = "";
                    Common.AddMemberActivity(userInfo.FullName, userInfo.UserID, "", ActivityType.NewMember);
                    ScriptManager.RegisterStartupScript(pnlUpdate, this.GetType(), "alert", "alert('Your have successfully registered with us. Please login to continue!');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(pnlUpdate, this.GetType(), "alert", "alert('Username already exists!');", true);
                }
            }
        }

    
    }
}